package com.keyin.widget;

import android.app.Activity;
import android.content.Context;
import android.graphics.Bitmap;
import android.view.Gravity;
import android.view.KeyEvent;
import android.view.View;
import android.view.Window;
import android.view.WindowManager;
import android.view.View.OnKeyListener;
import android.widget.AdapterView;
import android.widget.AdapterView.OnItemClickListener;
import android.widget.AdapterView.OnItemSelectedListener;
import android.widget.BaseAdapter;
import android.widget.PopupWindow;

import com.keyin.PastPeridocalsActivity;
import com.keyin.R;
import com.keyin.android.util.Device;
import com.keyin.android.view.coverflow.AbstractCoverFlowImageAdapter;
import com.keyin.android.view.coverflow.CoverFlow;
import com.keyin.android.view.coverflow.ReflectingImageAdapter;
import com.keyin.android.view.coverflow.ResourceImageAdapter;
import com.keyin.protocol.DownloadMagazine;
import com.keyin.protocol.NewPeriodicals;

public class NewMagazineCoverflow extends PopupWindow {

	private View root;
	private Activity context;
	private CoverFlow mCoverFlow;
	private NewPeriodicals news;

	public NewMagazineCoverflow(Activity context, final Bitmap[] bitmap, NewPeriodicals newMagazines) {
		// super(View.inflate(context, R.layout.alert, null), 1280, 800, true);
		super(context);
		this.context = context;
		this.news = newMagazines;

		setWidth(Device.getInstance().getDisplayMetrics(context).widthPixels);
		setHeight(Device.getInstance().getDisplayMetrics(context).heightPixels);
        setFocusable(true);

		mCoverFlow = (CoverFlow) View.inflate(context, R.layout.v_coverflow,
				null);
		mCoverFlow.setDefaultAttributes(context);

		BaseAdapter coverImageAdapter = new ReflectingImageAdapter(
				new AbstractCoverFlowImageAdapter() {

					@Override
					public int getCount() {
						return bitmap.length;
					}

					@Override
					protected Bitmap createBitmap(int position) {
						return bitmap[position];
					}
				});
		mCoverFlow.setAdapter(coverImageAdapter);
		mCoverFlow.setSelection(0, true);
		setupListeners(mCoverFlow);

		mCoverFlow.setOnKeyListener(new OnKeyListener() {

			@Override
			public boolean onKey(View v, int keyCode, KeyEvent event) {
				if (keyCode == KeyEvent.KEYCODE_BACK) {
					if (isShowing()) {
						dismiss();
					}
				}
				return false;
			}
		});

		setContentView(mCoverFlow);
	}

	/**
	 * Sets the up listeners.
	 * 
	 * @param mCoverFlow
	 *            the new up listeners
	 */
	private void setupListeners(final CoverFlow mCoverFlow) {
		mCoverFlow.setOnItemClickListener(new OnItemClickListener() {
			@Override
			public void onItemClick(final AdapterView<?> parent,
					final View view, final int position, final long id) {

				new DownloadMagazine(context, news.magazines.get(position));
			}

		});
		mCoverFlow.setOnItemSelectedListener(new OnItemSelectedListener() {
			@Override
			public void onItemSelected(final AdapterView<?> parent,
					final View view, final int position, final long id) {
			}

			@Override
			public void onNothingSelected(final AdapterView<?> parent) {
			}
		});
	}
}
